<template>
  <div class="app-container" style="padding: 10px">
    <el-card class="box-card" shadow="false">
      <el-tabs v-model="tab" @tab-click="handleQuery">
        <el-tab-pane label="未考试" name="0">
          <div>
            <el-form v-show="showSearch" ref="queryForm" :model="queryParams" :inline="true" label-width="80px">
              <el-form-item label="ID" prop="id" v-if="false">
                <el-input
                  v-model="queryParams.id"
                  placeholder="请输入考试任务ID"
                  clearable
                />
              </el-form-item>
              <el-form-item label="考试标题" prop="testPaperName">
                <el-input
                  v-model="queryParams.taskInfoTitle"
                  placeholder="请输入考试标题"
                  clearable
                />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" icon="el-icon-search" @click="handleQuery"> 搜索</el-button>
                <el-button icon="el-icon-refresh" @click="resetQuery"> 重置</el-button>
              </el-form-item>
            </el-form>

            <el-table v-loading="loading" :data="dataList" border>
              <el-table-column label="考试标题" prop="taskInfoTitle" sortable/>
              <el-table-column label="试卷总分" prop="testPaperFraction" width="80px" align="center"/>
              <el-table-column label="考试截止日期" prop="taskInfoEndDate" v-if="tab === '0'" width="220px" sortable
                               align="center"/>
              <el-table-column label="操作" width="120px" align="center">
                <template slot-scope="scope">
                  <el-button
                    icon="el-icon-edit-outline"
                    size="mini"
                    type="text"
                    @click="goExam(scope.row)">
                    开始考试
                  </el-button>
                </template>
              </el-table-column>
            </el-table>

            <pagination
              v-show="total > 0"
              :limit.sync="queryParams.pageSize"
              :page.sync="queryParams.pageNum"
              :total="total"
              @pagination="getList"
            />
          </div>
        </el-tab-pane>
        <el-tab-pane label="已考试" name="1">
          <div>
            <el-form v-show="showSearch" ref="queryForm" :model="queryParams" :inline="true" label-width="80px">
              <el-form-item label="ID" prop="id" v-if="false">
                <el-input
                  v-model="queryParams.id"
                  placeholder="请输入考试任务ID"
                  clearable
                />
              </el-form-item>
              <el-form-item label="考试标题" prop="testPaperName">
                <el-input
                  v-model="queryParams.taskInfoTitle"
                  placeholder="请输入考试标题"
                  clearable
                />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" icon="el-icon-search" @click="handleQuery"> 搜索</el-button>
                <el-button icon="el-icon-refresh" @click="resetQuery"> 重置</el-button>
              </el-form-item>
            </el-form>

            <el-table v-loading="loading" :data="dataList" border>
              <el-table-column label="考试标题" prop="taskInfoTitle" sortable/>
              <el-table-column label="考试时间" v-if="tab === '1'" width="260px" align="center">
                <template slot-scope="scope">
                  <div>
                    <span>开始时间：{{ scope.row.startTime }}</span>
                  </div>
                  <div>
                    <span>结束时间：{{ scope.row.endTime }}</span>
                  </div>
                </template>
              </el-table-column>
              <el-table-column label="试卷总分" prop="testPaperFraction" width="80px" align="center"/>
              <el-table-column label="得分" prop="rectifyScore" sortable width="120px" align="center"/>
              <el-table-column label="操作" width="120px" align="center">
                <template slot-scope="scope">
                  <el-button
                    icon="el-icon-view"
                    size="mini"
                    type="text"
                    @click="handleView(scope.row.id)"
                  >
                    查看
                  </el-button>
                  <el-button
                    icon="el-icon-view"
                    size="mini"
                    type="text"
                    @click="handleRecord(scope.row.id)"
                  >
                    记录
                  </el-button>
                </template>
              </el-table-column>
            </el-table>

            <pagination
              v-show="total > 0"
              :limit.sync="queryParams.pageSize"
              :page.sync="queryParams.pageNum"
              :total="total"
              @pagination="getList"
            />
          </div>
        </el-tab-pane>
        <el-tab-pane label="待重考" name="2">
          <div>
            <el-form v-show="showSearch" ref="queryForm" :model="queryParams" :inline="true" label-width="80px">
              <el-form-item label="ID" prop="id" v-if="false">
                <el-input
                  v-model="queryParams.id"
                  placeholder="请输入考试任务ID"
                  clearable
                />
              </el-form-item>
              <el-form-item label="考试标题" prop="testPaperName">
                <el-input
                  v-model="queryParams.taskInfoTitle"
                  placeholder="请输入考试标题"
                  clearable
                />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" icon="el-icon-search" @click="handleQuery"> 搜索</el-button>
                <el-button icon="el-icon-refresh" @click="resetQuery"> 重置</el-button>
              </el-form-item>
            </el-form>

            <el-table v-loading="loading" :data="dataList" border>
              <el-table-column label="考试标题" prop="taskInfoTitle" sortable/>
              <el-table-column label="试卷总分" prop="testPaperFraction" width="80px" align="center"/>
              <el-table-column label="操作" width="120px" align="center">
                <template slot-scope="scope">
                  <el-button
                    icon="el-icon-edit-outline"
                    size="mini"
                    type="text"
                    @click="goExam(scope.row)">
                    开始考试
                  </el-button>
                </template>
              </el-table-column>
            </el-table>

            <pagination
              v-show="total > 0"
              :limit.sync="queryParams.pageSize"
              :page.sync="queryParams.pageNum"
              :total="total"
              @pagination="getList"
            />
          </div>
        </el-tab-pane>
      </el-tabs>

      <el-dialog :visible.sync="scoreShow" title="查看考试任务" v-if="scoreShow" width="80%">
        <grade :id="infoId" view/>
      </el-dialog>

      <el-dialog :visible.sync="historyShow" title="查看考试记录" v-if="historyShow" width="400px">
        <el-table :data="historyList" border>
          <el-table-column label="考试时间" prop="createTime" align="center"/>
          <el-table-column label="得分" prop="score" sortable align="center"/>
        </el-table>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import {historyList, startExamCheck, testTaskList} from "@/api/exam/testTask";
import Grade from './grade.vue'

export default {
  name: 'myTask',
  components: {Grade},
  data() {
    return {
      tab: '0',
      loading: false,
      showSearch: true,
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },
      total: 0,
      dataList: [],
      info: null,
      scoreShow: false,
      historyShow: false,
      historyList: []
    }
  },
  created() {
    this.handleQuery()
  },
  methods: {
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10
      }
      this.getList()
    },
    getList() {
      this.queryParams.status = this.tab
      this.queryParams.userFlag = 1
      this.loading = true
      testTaskList(this.queryParams).then(res => {
        this.dataList = res.rows
        this.total = res.total
        this.loading = false
      })
    },
    goExam(row) {
      // 校验
      startExamCheck(row.id).then(res => {
        if (res.code === 200) {
          this.$router.push({
            path: '/exam/testTask/exam',
            query: {
              taskId: row.id,
              testPaperId: row.testPaperId
            }
          })
        } else {
          this.$message.error(res.message)
        }
      })
    },
    handleView(id) {
      this.infoId = id
      this.scoreShow = true
    },
    handleRecord(id) {
      historyList(id).then(res => {
        this.historyList = res.data
        this.historyShow = true
      })
    }
  }
}
</script>

<style scoped>

</style>
